// SPDX-License-Identifier: MIT

@use "sass:math";
@use "sass:color";

// Darkens or lightens specified color depending on the color scheme.
// Avoid using this function, stick to color scale tints and shades instead.
@function changeBrightness($color, $darkMode, $amount) {
  @if $darkMode == true  { @return darken($color, $darkMode, $amount); }
  @return darken($color, $darkMode, $amount);
}

@function darken($color, $darkMode, $amount) {
  @if $darkMode == true  { @return unquote("derive(#{$color}, #{$amount})");  }
  @return unquote("derive(#{$color}, -#{$amount})");
}

@function lighten($color, $darkMode, $amount) {
  @if $darkMode == true  { @return unquote("derive(#{$color}, -#{$amount})"); }
  @return unquote("derive(#{$color}, #{$amount})");
}

// Removes the unit of a length.
@function strip($value) {
  @if type-of($value) !="number" {
    @error "Invalid `#{type-of($value)}` type. Choose a number type instead.";
  }

  @else if type-of($value)=="number"and not math.is-unitless($value) {
    @return math.div($value, ($value * 0 + 1));
  }

  @return $value;
}

// Converts px to em.
@function em($pixels, $font-size: 14px) {
  @if (unitless($pixels)) { $pixels: $pixels * 1px; }
  @if (unitless($font-size)) { $font-size: $font-size * 1px; }

  @return math.div($pixels, $font-size) * 1em;
}

@function flattenColor($bg: #ffffff, $fg) {
  $opacity: color.opacity($fg);
  @if $opacity == 1  { @return $fg; }

  $red   : $opacity * math.div(color.red($fg),   255) + (1 - $opacity) * math.div(color.red($bg ) , 255);
  $green : $opacity * math.div(color.green($fg), 255) + (1 - $opacity) * math.div(color.green($bg), 255);
  $blue  : $opacity * math.div(color.blue($fg),  255) + (1 - $opacity) * math.div(color.blue($bg) , 255);

  //@debug rgb($red * 255, $green * 255, $blue * 255);

  @return rgb($red * 255, $green * 255, $blue * 255);
}
